<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//

require_once($WSC->get_site_inc_dir().'modules/chocolate_ajax_form_ctrl/chocolate_ajax_form_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tple_html_gatherer/tple_html_gatherer.class.php');


abstract class Caramel_Ajax_Form_Ctrl extends Chocolate_Ajax_Form_Ctrl {
	public function get_tvm() {
		$svm = $this->get_svm();

		$tm = $svm->get_global_var(THREADS_MANAGER_NAME);
		$tvm = $tm->get_current_thread_vm();

		return $tvm;
	}


	public function query_tvm() {
		$svm = $this->get_svm();

		$tm = $svm->get_global_var(THREADS_MANAGER_NAME);
		$tvm = $tm->get_current_thread_vm();

		return $tvm;
	}


	/**
	 * Parses TPL file and returns HTML
	 *
	 * @param string $tpl_file Path to form tpl file
	 * @return string
	 */
	protected function _get_html($tpl_file) {
		$svm = $this->get_svm();
		$tm = $svm->get_global_var(THREADS_MANAGER_NAME);

		$html_gatherer = new Tple_HTML_Gatherer($tpl_file);
		$form_ctrl = $this->get_form_ctrl();
		$this->add_exports($form_ctrl->get_tple_exports());

		$this->export($tm->get_url_rewrite_var_name(), $tm->get_current_url_var());

		$config = $this->get__config();
		$this->export('_language', $config->get_current_language());

		$ret = $html_gatherer->get_html($this->get__exports());
		$ret = $this->rewrite_html_with_transit_vars($ret);

		return $ret;
	}


	protected function send_response($json, $html = ' ') {
		$svm = $this->get_svm();
		$tm = $svm->get_global_var(THREADS_MANAGER_NAME);

		$this->add_additional_response_parameter('_tm', $tm->get_current_url_var());

		parent::send_response($json, $html);
	}
}
